package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName : GoodsController  //类名
 * @Description :   //描述
 * @Author : qingfeng  //作者
 * @Date: 2020-09-18 10:58  //时间
 */
@RestController
@RequestMapping("goods")
public class GoodsController {

    private final SpuService spuService;
    private final SpuDetailService detailService;
    private final SkuService skuService;

    public GoodsController(SpuService spuService, SpuDetailService detailService, SkuService skuService) {
        this.spuService = spuService;
        this.detailService = detailService;
        this.skuService = skuService;
    }

    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> querySpuByPage(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                                          @RequestParam(value = "rows",defaultValue = "5")Integer rows,
                                                          @RequestParam(value = "saleable",defaultValue = "1")Boolean saleable,
                                                          @RequestParam(value = "categoryId",required = false)Long categoryId,
                                                          @RequestParam(value = "brandId",required = false)Long brandId,
                                                          @RequestParam(value = "id",required = false)Long id){
        /**
         * @Description //TODO 分页查询sku
         * @Param [page, rows, saleable, categoryId, brandId, id]
         * @return org.springframework.http.ResponseEntity<com.leyou.common.dto.PageDTO < com.leyou.item.dto.SpuDTO>>
         */
        return ResponseEntity.ok(spuService.querySpuByPage(page,rows,saleable,categoryId,brandId,id));
    }

    @PostMapping("spu")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO){
        /**
         * @Description //TODO 新增商品
         * @Param [spuDTO]
         * @return org.springframework.http.ResponseEntity<java.lang.Void>
         */
        spuService.saveGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @PutMapping("saleable")
    public ResponseEntity<Void> updateSpuSaleable(@RequestParam("id")Long id,
                                                  @RequestParam("saleable")Boolean saleable){
        /**
         * @Description //TODO 修改上下架状态
         * @Param [id, saleable]
         * @return org.springframework.http.ResponseEntity<java.lang.Void>
         */
        spuService.updateSaleable(id,saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryGoodsById(@PathVariable("id")Long id){
        /**
         * @Description //TODO 根据id查询商品
         * @Param [id]
         * @return org.springframework.http.ResponseEntity<com.leyou.item.dto.SpuDTO>
         */
        return ResponseEntity.ok(spuService.queryGoodsById(id));
    }

    @PutMapping
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO){
        /**
         * @Description //TODO 更新商品
         * @Param [spuDTO]
         * @return org.springframework.http.ResponseEntity<java.lang.Void>
         */
        spuService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailById(@RequestParam("id")Long id){
        /**
         * @Description //TODO 根据id查询detail
         * @Param [id]
         * @return org.springframework.http.ResponseEntity<com.leyou.item.dto.SpuDetailDTO>
         */
        return ResponseEntity.ok(new SpuDetailDTO(detailService.getById(id)));
    }

    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("id")Long id){
        /**
         * @Description //TODO 根据spuId查询sku
         * @Param [id]
         * @return org.springframework.http.ResponseEntity<java.util.List < com.leyou.item.dto.SkuDTO>>
         */
        return ResponseEntity.ok(
                SkuDTO.convertEntityList(
                        skuService.query().eq("spu_id",id).list()
                )
        );
    }

    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("ids")List<Long> ids){
        /**
         * @Description //TODO 根据id批量查询sku
         * @Param [ids]
         * @return org.springframework.http.ResponseEntity<java.util.List < com.leyou.item.dto.SkuDTO>>
         */
        return ResponseEntity.ok(
                SkuDTO.convertEntityList(skuService.listByIds(ids))
        );
    }

    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> querySpecValues(@RequestParam("id")Long id,
                                                              @RequestParam(value = "searching",required = false)Boolean searching){
        /**
         * @Description //TODO 根据spuId查询spu的所有规格参数值
         * @Param [id, searching]
         * @return org.springframework.http.ResponseEntity<java.util.List < com.leyou.item.dto.SpecParamDTO>>
         */
        return ResponseEntity.ok(detailService.querySpecValues(id,searching));
    }
    
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id")Long id){
        /**
         * @Description //TODO 根据id查询商品
         * @Param [id]
         * @return org.springframework.http.ResponseEntity<com.leyou.item.dto.SpuDTO>
         */
        return ResponseEntity.ok(new SpuDTO(spuService.getById(id)));
    }

    @PutMapping("/stock/minus")
    public ResponseEntity<Void> deductStock(@RequestBody Map<Long,Integer> cartMap){
        /**
         * @Description //TODO 扣减库存
         * @Param [cartMap]
         * @return org.springframework.http.ResponseEntity<java.lang.Void>
         */
        skuService.deductStock(cartMap);
        return ResponseEntity.noContent().build();
    }

    @PutMapping("/stock/plus")
    public ResponseEntity<Void> addStock(@RequestBody Map<Long,Integer> cartMap){
        /**
         * @Description //TODO 加库存
         * @Param [cartMap]
         * @return org.springframework.http.ResponseEntity<java.lang.Void>
         */
        skuService.addStock(cartMap);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
